<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);
pc_base::load_sys_class('form', '', 0);
pc_base::load_app_class('global');

class dynamic extends admin{
	private $db;
	private $file_db;
	
	public function __construct(){
		parent::__construct();
		$this->db = pc_base::load_model('dynamic_model');
		$this->file_db = pc_base::load_model('dynamic_file_model');
		$this->siteid = get_siteid();//获取当前站点
		$this->admin_userid = param::get_cookie('userid'); // 获取当前登录的管理员ID
	}
	
	public function init() {
		$page = $_GET['page'] ? intval($_GET['page']) : 1;
		$where = "status =1 ";
		if (isset($_GET['dosubmit'])) {
			$dynamic = $_GET['dynamic'];
			if (trim($dynamic['username']) != "") {
				$keyword = trim($dynamic['username']);
				$where .= "and user_name LIKE '%$keyword%' ";
			}
			if ($dynamic['start_time'] != "") {
				$start_time = $dynamic['start_time'];
				$where .= "and create_time > '$start_time' ";
			}
			if ($dynamic['end_time'] != "") {
				$end_time = $dynamic['end_time'];
				$where .= "and create_time < '$end_time'";
			}
		}
		$info = $this->db->listinfo($where,'id DESC',$page,10);
		$pages = $this->db->pages;
		pc_base::load_sys_class('format', '', 0);
		include $this->admin_tpl('dynamic_list');
	}
	
	public function del() {
		if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		$id = $_REQUEST['id'];
		if (is_array($id)) {
			foreach ($id as $k => $v) {
				if (intval($v)) {
					$id[$k] = intval($v);
				} else {
					unset($id[$k]);
				}
			}
			$id = implode('\',\'', $id);
		}
		$result = $this->db->update(array('status'=>2),"id in ('$id')");
		
		if ($result) {
			$this->refreshData(8);
			$id = str_replace("'", "", $id);
			//记录行为日志
			$log_content = '<span style="color:red;">删除动态</span>[动态id：'.$id.']';
			$this->manage_admin_log($log_content);
			showmessage(L('success'), HTTP_REFERER);
		} else {
			showmessage(L('error'), HTTP_REFERER);
		}
	}
	
	public function view() {
		$id = $_GET['id'] ? intval($_GET['id']) : showmessage('资源不存在',HTTP_REFERER);
		$dynamic_info = $this->db->get_one(array('id'=>$id));
		$sql = "select df.* from nsrweb_talent_dynamic as d LEFT JOIN nsrweb_talent_dynamic_file as df on df.dynamic_id=d.id where d.id=".$id." order by df.sort DESC";
		$this->db->query($sql);
		$file_info = $this->db->fetch_array();
		$show_header = false;
		$show_pc_hash = false;
		include $this->admin_tpl('dynamic_detail');
	}
	
	/**
	 * 删除动态后刷新缓存
	 */
	private function refreshData($type) {
		file_get_contents(API_URL.'/xinsilu/v1/refreshRedis/refreshRedis?type='.$type);
	}
}